﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System;


public class AndroidControl : MonoBehaviour
{
    private AndroidJavaObject jo;

    public Button btn_wxpay;
    public Button btn_alipay;
    public Button btn_changePayType;
    public Text text_PayType;

    private int w;
    private int h;

    private String BASE_URL = "http://pay.qaopay.com/form/pay?";
    private String returnUrl = "http://pay.jtpay.com/Form/TestReturn";
    private String noticeUrl = "http://pay.jtpay.com//Form/TestNotice";
    private static String payType="3" ; // 支付方式
    private String CUSTOM_NAME = "name"; // 客户名称
    private String CUSTOM_IP = "ip"; // 客户ip
    private String code = "6010200789"; // 商户号
    public  String price = "0.1"; // 价格

    private String comKey = "D1C8CAB76BFEFA0C713B60CAAABA1A9B"; // 支付密钥
    private String TERMINAL = "3"; // 终端设备固定值 ANDROID 3 IOS 2
    private String ISWAPPAY = "5"; // 支付场景固定值5
    private String[][] OPTION_PARAM = {new String[]{"p8_signtype", "1"}, new String[]{"p10_paychannelnum", "1"}, new String[]{"p11_cardtype", ""},
            new String[]{"p12_channel", ""}, new String[]{"p13_orderfailertime", ""}, new String[]{"p15_customcontacttype", ""},
            new String[]{"p16_customcontact", ""}, new String[]{"p18_product", ""}, new String[]{"p19_productcat", ""},
            new String[]{"p20_productnum", ""}, new String[]{"p21_pdesc", ""}, new String[]{"p22_version", ""},
            new String[]{"p23_charset", ""}, new String[]{"p24_remark", "1"}};
    // Use this for initialization
    void Start()
    {
        //获取Android的Java接口  
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        jo = jc.GetStatic<AndroidJavaObject>("currentActivity");

        //获取屏幕宽高
        w = Screen.width;
        h = Screen.height;


        //找到button按钮
        btn_alipay = GameObject.FindGameObjectWithTag("wappayActivity").GetComponent<Button>();
        btn_wxpay = GameObject.FindGameObjectWithTag("wappayWebview").GetComponent<Button>();
        btn_changePayType = GameObject.Find("changePayType").GetComponent<Button>();

        //退出按钮
        Button exit_btn = GameObject.Find("exit").GetComponent<Button>();
        exit_btn.transform.position = new Vector3(w - 40, 40);

        text_PayType = GameObject.Find("labelPayType").GetComponent<Text>();
    }

    // Update is called once per frame
    void Update()
    {

    }

    //wappayActivity
    public void wappayActivity()
    {
        String orderTime = DateTime.Now.ToString("yyyyMMddHHmmss");
        String order = "TestOrder_" + orderTime;
        String md5_data = code + "&" + order + "&" + price +
                "&" + returnUrl + "&" + noticeUrl + "&" + orderTime + comKey;

        String p7_sign = Assets.Md5.UserMd5(md5_data);

        jo.Call("wappayActivity", new object[] {price, code, p7_sign, returnUrl, noticeUrl, BASE_URL, payType,
                order, orderTime, CUSTOM_NAME, CUSTOM_IP, TERMINAL, ISWAPPAY });
        
    }


    //wappayWebview
    public void wappayWebview()
    {
        String orderTime = DateTime.Now.ToString("yyyyMMddHHmmss");
        String order = "TestOrder_" + orderTime;
        String md5_data = code + "&" + order + "&" + price +
                "&" + returnUrl + "&" + noticeUrl + "&" + orderTime + comKey;

        String p7_sign= Assets.Md5.UserMd5(md5_data);
        //Hashtable optionalMap = new Hashtable();
        String optionalStr="";
        for (int i=0;i<OPTION_PARAM.Length;i++)
        {
            String[] param = OPTION_PARAM[i];
            if (param[1]!= String.Empty)
            {
                //optionalMap.Add(param[0], param[1]);
                optionalStr +=param[0] + "=" + param[1]+"|";
            }
        }
        jo.Call("showToast", new object[] { optionalStr });
        
        jo.Call("wappayWebview", price, code, p7_sign, returnUrl, noticeUrl, BASE_URL, payType,
                order, orderTime, CUSTOM_NAME, CUSTOM_IP, TERMINAL, ISWAPPAY, optionalStr);
    }
    //changePayType
    public void changePayType()
    {
        
        if (text_PayType.text.Equals("微信"))
        {
            text_PayType.text = "支付宝";
            payType = "4";
        }
        else
        {
            text_PayType.text = "微信";
            payType = "3";
        }

    }


    //退出方法
    public void Exit()
    {//win和android 有效
        Application.Quit();
    }
}
